package com.sy.musicapp

import android.content.Context
import android.content.Intent
import android.util.Log
import android.widget.Toast
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.ViewModelStoreOwner

/**
 * 工具类
 * @author SY
 * @since 2022-01-22 22:04
 **/
object Util {
    enum class LogLevel {
        VERBOSE,
        DEBUG,
        INFO,
        WARN,
        ERROR,
        ASSERT
    }

    fun showLog(log: Any?, tag: String = "SYTAG", level: LogLevel = LogLevel.VERBOSE) {
        val current = Throwable().stackTrace[2]
        val fileName = current.fileName
        val lineNumber = current.lineNumber
        val decorLog = "($fileName:$lineNumber) ===> ${log.toString()}"
        when (level) {
            LogLevel.VERBOSE -> Log.v(tag, decorLog)
            LogLevel.DEBUG -> Log.d(tag, decorLog)
            LogLevel.INFO -> Log.i(tag, decorLog)
            LogLevel.WARN -> Log.w(tag, decorLog)
            LogLevel.ERROR -> Log.e(tag, decorLog)
            LogLevel.ASSERT -> Log.wtf(tag, decorLog)
        }

    }
}


fun dpTopx(value: Float): Int {
    val scale = MyApplication.context.resources.displayMetrics.density
    return (value * scale + 0.5f).toInt()
}

fun String.showToast(duration: Int = Toast.LENGTH_SHORT) {
    Toast.makeText(MyApplication.context, this, duration).show()
}

fun <T> intent(packageContext: Context, serviceClass: Class<T>): Intent =
    Intent(packageContext, serviceClass)

inline fun <reified T> MyIntent(context: Context): Intent = intent(context, T::class.java)

fun <T : ViewModel> createModel(
    context: ViewModelStoreOwner,
    viewModelClass: Class<T>
): T = ViewModelProvider(
    context,
    ViewModelProvider.NewInstanceFactory()
).get(viewModelClass)

inline fun <reified T : ViewModel> createModel(context: ViewModelStoreOwner): T =
    createModel(context, T::class.java)